-- This is a program with an animed rectangle and
-- another rectangle that can be moved with the joypad.

function OnInit()
  AnimatedRectX = 1
  AnimatedRectY = 1
  MovableRectX = 1
  MovableRectY = 1
  
  SetColor(255, 25, 255)
  FillRect(0, 0, ScreenWidth(), ScreenHeight())
  UpdateScreen()
  
  SetClockInterval(50)
end

function OnTouchDown(x, y)
  MovableRectX = x - 50
  MovableRectY = y - 50
  UpdateScreen()
end

function OnTouchDrag(x, y)
  OnTouchDown(x, y)
end

function OnKeyDown(keyCode)
  KeyCode = keyCode
  if keyCode == 273 then -- Up 50
    MovableRectY = MovableRectY - 10
  end
  if keyCode == 274 then -- Down 56
    MovableRectY = MovableRectY + 10
  end
  if keyCode == 275 then -- Right 54
    MovableRectX = MovableRectX + 10
  end
  if keyCode == 276 then -- Left 52
    MovableRectX = MovableRectX - 10
  end
  DrawScreen()
end

function OnClockTick()
  AnimatedRectY = AnimatedRectY + 10
  if AnimatedRectY > ScreenHeight() then
    AnimatedRectY = -100
  end
  DrawScreen()
end

function DrawScreen()
  SetColor(255, 255, 255)
  FillRect(0, 0, ScreenWidth(), ScreenHeight())
  
  SetColor(0, 255, 255)
  FillRect(AnimatedRectX, AnimatedRectY, 100, 100)
  
  SetColor(0, 255, 0)
  FillRect(MovableRectX, MovableRectY, 100, 100)
  
  DrawText('Key code: ' .. KeyCode, 20, 20, 'Arial32Black')
  
  UpdateScreen()
end

